package com.ruoyi.qiuyue.handler;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.qiuyue.handler.exception.RedisCodeException;
import com.ruoyi.qiuyue.handler.exception.UserExistsException;
import com.ruoyi.qiuyue.handler.exception.UserNullException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常拦截
 */
@RestControllerAdvice
public class WebExceptionHandler {
    /**
     * 用户空异常
     * @param e
     * @return
     */
    @ExceptionHandler(UserNullException.class)
    public AjaxResult UserNull(UserNullException e) {
        return AjaxResult.success(e.getMsg());
    }

    /**
     * 验证码错误
     * @param e
     * @return
     */
    @ExceptionHandler(RedisCodeException.class)
    public AjaxResult CodeError(RedisCodeException e) {
        return AjaxResult.success(e.getMsg());
    }

    /**
     * 用户已存在
     * @param e
     * @return
     */
    @ExceptionHandler(UserExistsException.class)
    public AjaxResult CodeError(UserExistsException e) {
        return AjaxResult.success(e.getMsg());
    }

    /**
     * 全局异常
     * @param e
     * @return
     */
    @ExceptionHandler(RuntimeException.class)
    public AjaxResult allException(RuntimeException e) {
        return AjaxResult.error(e.getMessage());
    }
}
